package com.xiaoyu.lanling.activity.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.fragment.app.Fragment;

import com.xiaoyu.base.app.AppEngine;

import in.srain.cube.app.lifecycle.IComponentContainer;
import in.srain.cube.app.lifecycle.LifeCycleComponent;
import in.srain.cube.app.lifecycle.LifeCycleComponentManager;
import in.srain.cube.util.CLog;

public class BaseFragment extends Fragment implements IComponentContainer {

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    private LifeCycleComponentManager mComponentContainer = new LifeCycleComponentManager();
    private int mResumeTime = 0;
    private int mStartTime = 0;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        showStatus("onActivityCreated");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        showStatus("onAttachFragment");
        super.onAttachFragment(childFragment);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        showStatus("onCreateView");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        showStatus("onViewCreated");
        super.onViewCreated(view, savedInstanceState);

        if (AppEngine.getInstance().hasFinishedInitialJob()) {
            doOnViewCreatedAfterAppFinishInit(view, savedInstanceState);
        } else {
            AppEngine.getInstance().addFinishInitialJobCallback(() -> {
                doOnViewCreatedAfterAppFinishInit(view, savedInstanceState);
            });
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        showStatus("onSaveInstanceState");
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        showStatus("onViewStateRestored");
        super.onViewStateRestored(savedInstanceState);
    }

    @Override
    public void onStart() {
        showStatus("onStart");
        super.onStart();
        mStartTime++;
        final boolean isFirstTimeStart = mStartTime == 1;

        if (AppEngine.getInstance().hasFinishedInitialJob()) {
            doOnStartAfterAppFinishInit(isFirstTimeStart);
        } else {
            AppEngine.getInstance().addFinishInitialJobCallback(() -> {
                doOnStartAfterAppFinishInit(isFirstTimeStart);
            });
        }
    }

    @Override
    public void onResume() {
        showStatus("onResume");
        super.onResume();
        mComponentContainer.onBecomesVisibleFromPartiallyInvisible();

        mResumeTime++;
        final boolean isFirstTimeResume = mResumeTime == 1;

        if (AppEngine.getInstance().hasFinishedInitialJob()) {
            doOnResumeAfterAppFinishInit(isFirstTimeResume);
        } else {
            AppEngine.getInstance().addFinishInitialJobCallback(() -> {
                doOnResumeAfterAppFinishInit(isFirstTimeResume);
            });
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        showStatus("onHiddenChanged  " + hidden);
        super.onHiddenChanged(hidden);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        showStatus("setUserVisibleHint  " + isVisibleToUser);
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onPause() {
        mComponentContainer.onBecomesPartiallyInvisible();
        showStatus("onPause");
        super.onPause();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        showStatus("onCreate");
        super.onCreate(savedInstanceState);

        if (AppEngine.getInstance().hasFinishedInitialJob()) {
            doOnCreateAfterAppFinishInit(savedInstanceState);
        } else {
            AppEngine.getInstance().addFinishInitialJobCallback(() -> {
                doOnCreateAfterAppFinishInit(savedInstanceState);
            });
        }
    }

    @Override
    public void onStop() {
        mComponentContainer.onBecomesTotallyInvisible();
        showStatus("onStop");
        super.onStop();
    }

    @Override
    public void onDestroy() {
        mComponentContainer.onDestroy();
        showStatus("onDestroy");
        super.onDestroy();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        showStatus("onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void addComponent(LifeCycleComponent component) {
        mComponentContainer.addComponent(component);
    }

    protected void showStatus(String status) {
        final String[] className = ((Object) this).getClass().getName().split("\\.");
        CLog.d("cp-lifecycle", "%s %s %s", className[className.length - 1], status, this);
    }

    private void doOnCreateAfterAppFinishInit(Bundle savedInstanceState) {
        showStatus("onCreateSafelyAfterAppFinishInit");
        onCreateSafelyAfterAppFinishInit(savedInstanceState);
    }

    private void doOnStartAfterAppFinishInit(boolean isFirstTimeStart) {
        showStatus("onStartSafelyAfterAppFinishInit");
        onStartSafelyAfterAppFinishInit(isFirstTimeStart);
    }

    private void doOnResumeAfterAppFinishInit(boolean isFirstTimeResume) {
        showStatus("onResumeSafelyAfterAppFinishInit");
        onResumeSafelyAfterAppFinishInit(isFirstTimeResume);
    }

    private void doOnViewCreatedAfterAppFinishInit(@NonNull View view, @Nullable Bundle savedInstanceState) {
        showStatus("doOnViewCreatedAfterAppFinishInit");
        onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState);
    }

    protected void onCreateSafelyAfterAppFinishInit(@Nullable Bundle savedInstanceState) {
    }

    protected void onStartSafelyAfterAppFinishInit(boolean isFirstTimeStart) {
    }

    protected void onResumeSafelyAfterAppFinishInit(boolean isFirstTimeResume) {

    }

    protected void onViewCreatedSafelyAfterAppFinishInit(@NonNull View view, @Nullable Bundle savedInstanceState) {

    }
}
